<?php defined('SYSPATH') OR die('No direct access allowed.');

class Order_Model extends ORM 
{
	protected $belongs_to = array('user', 'product');

	public function save()
	{
		if ($this->loaded === FALSE)
		{
			// Set the created time, token, and hash of the user agent
			$this->create_time = time();
			$this->pay_id = $this->create_order_id();
			echo $this->pay_id;
		}

		return parent::save();
	}

	private function create_order_id()
	{
		while (TRUE)
		{
			// Create a random order_id 
			$order_id = uniqid();

			// Make sure the token does not already exist
			if ($this->db->select('id')->where('pay_id', $order_id)->get($this->table_name)->count() === 0)
			{
				// A unique token has been found
				return $order_id;
			}
		}
	}
} // End Order Model

